/*
 * ListeningThread.cpp
 *
 *  Created on: Nov 2, 2012
 *      Author: lucas
 */

#include "ListeningThread.h"
#include "Server.h"
#include <iostream>
#include "Client.h"
ListeningThread::~ListeningThread() {
}

ListeningThread::ListeningThread(int port, Server& s) : lsck(port), server(s) {
}

void ListeningThread::ejecutar() {
	Client* client;
	CommunicationSocket* csck;

	lsck.escuchar();
	while (this->running()) {
		csck = lsck.aceptar();
		if (csck) {
			client = new Client(csck, server);
			client->iniciar();
			server.agregarCliente(client);
			std::cout<<"Se conecto un cliente"<<std::endl;
			server.quitarClientesDesconectados();
		}
		else
			std::cout<<"Tenemos un problema"<<std::endl;
	}
	std::cout<<"Salio"<<std::endl;
	lsck.cerrar();
}

void ListeningThread::parar() {
	Thread::parar();
	lsck.shutDown();
}
